package com.tbb.app.common.exception;

import com.tbb.app.common.entity.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import javax.validation.ConstraintViolationException;


/**
 * @author LiuSenLin
 * @date 2020/1/16
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public BaseResponse<Object> deal(Exception e) {
        BaseResponse<Object> result = BaseResponse.failed();
        log.error(e.getMessage(), e);
        if (e instanceof BusinessException) {
            // 系统业务异常
            BusinessException base = (BusinessException) e;
            result.setMessage(base.getMessage());
            result.setData(base.getData());
            result.setCode(CommonCode.C00001.getCode());
        } else if (e instanceof ParamValidException) {
            // 系统参数验证异常
            ParamValidException base = (ParamValidException) e;
            result.setMessage(base.getMessage());
            result.setData(base.getData());
            result.setCode(CommonCode.C00003.getCode());
        } else if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException validException = (MethodArgumentNotValidException) e;
            BindingResult bindingResult = validException.getBindingResult();
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            result.setMessage(defaultMessage);
            result.setData(null);
            result.setCode(CommonCode.C00003.getCode());
        } else if (e instanceof ConstraintViolationException) {
            ConstraintViolationException base = (ConstraintViolationException) e;
            String message = base.getConstraintViolations().iterator().next().getMessage();
            result.setMessage(message);
            result.setData(null);
            result.setCode(CommonCode.C00003.getCode());
        } else if (e instanceof HttpMessageNotReadableException) {
            result.setMessage("入参格式错误");
            result.setData(null);
            result.setCode(CommonCode.C00003.getCode());
        } else if (e instanceof MissingServletRequestParameterException) {
            MissingServletRequestParameterException base = (MissingServletRequestParameterException) e;
            String message = base.getMessage();
            result.setMessage(message);
            result.setData(null);
            result.setCode(CommonCode.C00003.getCode());
        } else if (e instanceof MissingServletRequestPartException) {
            MissingServletRequestPartException base = (MissingServletRequestPartException) e;
            String message = base.getMessage();
            result.setMessage(message);
            result.setData(null);
            result.setCode(CommonCode.C00003.getCode());
        }
        return result;
    }
}
